package org.particlethink.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.particlethink.auth.DefaultUser;
import org.particlethink.context.Context;
import org.particlethink.context.DefaultContext;
import org.particlethink.mapper.MasterPlanCategoryMapper;
import org.particlethink.model.MasterPlanCategory;
import org.particlethink.model.User;
import org.particlethink.response.BaseResponse;
import org.particlethink.response.MasterPlanCategoryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "总计划大类")
@RestController
@RequestMapping("/master_plan_category")
public class MasterPlanCategoryController {

    @Autowired
    public MasterPlanCategoryMapper masterPlanCategoryMapper;

    @GetMapping("/list")
    public BaseResponse<List<MasterPlanCategoryResponse>> list() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        DefaultUser defaultUser = (DefaultUser) authentication.getPrincipal();
        User user = defaultUser.getUser();

        Context ctx = DefaultContext.Builder.background().withValue("token", ((DefaultUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getToken());
        List<MasterPlanCategory> masterPlanCategories = masterPlanCategoryMapper.all(user.getToken());
        List<MasterPlanCategoryResponse> collect = masterPlanCategories.stream().map(MasterPlanCategoryResponse::new).collect(Collectors.toList());
        return BaseResponse.success(collect);

    }
}
